package com.itheima.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.SetMeal;
import com.itheima.service.SetMealService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 新增
     *
     * @param setMeal
     * @return
     */
    @PostMapping("")
    public R<String> save(@RequestBody SetMeal setMeal) {

        Boolean res = setMealService.save(setMeal);

        return R.success("新增套餐成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> getPage(@RequestParam(required = false) Integer page,
                           @RequestParam(required = false) Integer pageSize,
                           @RequestParam(required = false) String name) {


        return R.success(setMealService.getPage(page, pageSize, name));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetMeal> getById(@PathVariable String id) {
        if (StrUtil.isNotEmpty(id)) {
            SetMeal setMeal = setMealService.getById(id);


            return R.success(setMeal);
        }

        return R.error("id不存在");
    }

    /**
     * 修改
     *
     * @param setMeal
     * @return
     */
    @PutMapping("")
    public R<String> modify(@RequestBody SetMeal setMeal) {

        Boolean res = setMealService.modifyById(setMeal);

        return res ? R.success("修改成功") : R.error("菜品不能为空");
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("")
    public R<String> remove(String[] ids) {

        Boolean res = setMealService.remove(ids);
        R<String> result = res ? R.success("成功") : R.error("套餐正在售卖中，不能删除");
        return result;
    }

    /**
     * 上下架
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable("status") Integer status,
                                  @RequestParam("ids") String ids) {

        if (StrUtil.isNotEmpty(ids)) {
            Boolean res = setMealService.changeStatus(ids, status);

            return R.success("操作成功");
        }
        return R.error("套餐不存在");
    }


}
